<?php
namespace app\home\controller;

use app\home\common\Common;
use app\home\common\Page;
use app\home\common\Pagetwo;
use app\infrastructure\uri\Request;
use app\infrastructure\uri\Uri;

/**
 * Created by PhpStorm.
 * User: YD
 * Date: 2017/4/26
 * Time: 16:52
 */
class Active extends Common
{
    private $client;
    public  function  __construct(){
        parent::__construct();
        $this->client=new Request();
    }
    public function bannerList()
    {
        $p=empty(input('p'))?1:input('p');
        $startIndex=($p-1)*10;
        $countUrl=Uri::HOST.Uri::SELECTBANNER;
        $url=Uri::HOST.Uri::SELECTBANNER."?limitation=10&offset=".$startIndex;
        $value = $this->client->getArrayData($url);
        $totalCount =$this->client->getArrayData($countUrl);
        if($value['code'] != 0){
            $value['data']=[];
        }
        $pager=new Page(count($totalCount['data']),10,$p);
        $page=$pager->show();
         $this->assign('page',$page);
         $this->assign('data',$value['data']);
//        dd($value['data']);
         $this->assign('totalCount',count($totalCount['data']));
        return view();
    }
    //添加banner
    public function addBanner(){
        if(\think\Request::instance()->isPost()){
          if(empty($_FILES)){
              return json_encode(['code'=>3,'msg'=>'please upload photo','data'=>'please upload photo']);
          }
            if(empty($_POST['plantForm'])){
              return json_encode(['code'=>3,'msg'=>'please input plantForm','data'=>'please input plantForm']);
          }
            if(empty($_POST['url'])){
              return json_encode(['code'=>3,'msg'=>'please input url','data'=>'please input url']);
          }
           $res=$this->uploadFile('file');
            if($res['valid'] == 0){
                $data=[
                    "url"=>$_POST['url'],
                    "is_valid"=>$_POST['is_valid'],
                    "operator_id"=>session('userid'),
                    "operator_name"=>session('username'),
                    "sort"=>$_POST['sort'],
                    "img_path"=>$res['data'],
                    "title"=>$_POST['title'],
                    "plantForm"=>$_POST['plantForm'],
                    "comment"=>$_POST['comment'],
                    "start_time"=>$_POST['start_time'],
                    "end_time"=>$_POST['end_time'],
                    "imgcontent"=>'',
                ];
                $createBanerUrl=Uri::HOST.Uri::CREATEBANNER;
                $res=$this->client->getArrayData($createBanerUrl,$data,3);
                $res=json_encode($res);
//                dd($res);die;
                return $res;
            }else{
                return json_encode(['code'=>3,'msg'=>'0x300','data'=>$res['data']]);
            }
        }
        return view();
    }
    //banner有效性设置
    public function updateBanner(){
        $data=[
            'is_valid'=>$_POST['is_valid']
        ];
        $url=Uri::HOST.Uri::UPDATEBANNER.$_POST['uid'];
        $res=$this->client->getArrayData($url,$data,4);
        return json_encode($res);
    }

    //修改banner
    public function editBanner(){

        if(\think\Request::instance()->isPost()){
            $id=$_POST['uid'];
          if(empty($_FILES)){
              $data=[
                  "url"=>$_POST['url'],
                  "is_valid"=>$_POST['is_valid'],
                  "sort"=>$_POST['sort'],
                  "title"=>$_POST['title'],
                  "plantForm"=>$_POST['plantForm'],
                  "comment"=>$_POST['comment'],
                  "start_time"=>$_POST['start_time'],
                  "end_time"=>$_POST['end_time']
              ];
          }else{
              $res=$this->uploadFile('file');
              if($res['valid'] == 0){
                  $data=[
                      "url"=>$_POST['url'],
                      "is_valid"=>$_POST['is_valid'],
                      "sort"=>$_POST['sort'],
                      "img_path"=>$res['data'],
                      "title"=>$_POST['title'],
                      "plantForm"=>$_POST['plantForm'],
                      "comment"=>$_POST['comment'],
                      "start_time"=>$_POST['start_time'],
                      "end_time"=>$_POST['end_time'],
                  ];
              }else{
                  return json_encode(['code'=>3,'msg'=>'0x300','data'=>$res['data']]);
              }
          }
            $createBanerUrl=Uri::HOST.Uri::UPDATEBANNER.$id;
            $res=$this->client->getArrayData($createBanerUrl,$data,4);
            return json_encode($res);
        }
        $id=$_GET['uid'];
        $url=Uri::HOST.Uri::SELECTBANNER.'?id='.$id;
        $bannerInfo=$this->client->getArrayData($url);
        $this->assign('oldData',$bannerInfo['data'][0]);
//        dd($bannerInfo);die;
        return view();
    }
}